/**
 * 安全奖励申请 列表页
 *
 * 作者 wanghjw
 * 日期 2024-09-03 14:08:36
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-incentiveRewardView'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#incentiveRewardView-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate();
    initSelect();
    initTable();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            range: ['#start-date', '#end-date'],
            format: 'yyyy-MM-dd',
            btns:['now','confirm']
        })
        $('#end-date').val(commonJS.getCurrentDateStr(new Date()));
        $('#start-date').val(commonJS.getPastDateStr(new Date(),7));
        form.render();

    }

    function initSelect() {
        var data ={
            status:'1',
            tenantId:getTenantId(),
            year:$searchForm.find('input[name="year"]').val()
        };
        $("#applyUserId").empty();
        //初始化商品单位
        febs.getSync(ctx + 'incentiveQuotaConfig', data, function (r) {
            console.log("项目列表" + JSON.stringify(r.data))
            var areaLis = r.data;
            var options = '<option value="">请选择</option>';
            $.each(areaLis, function (index, item) {
                options += '<option value="' + item.userId + '">' + item.realName + '</option>'
            })
            $("#applyUserId").append(options);
        });
        form.render();
    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'incentiveRewardViewTable',
            url: ctx + 'incentiveRewardView/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [
                [
                    {type: 'numbers', title: '序号'},
                    {
                        field: 'tenantId',title: '公司名称',minWidth: 100,hide: !hasHeadCompanyRole,
                        templet: function (d) {
                            if(d.tenantId){
                                var tenant = tenantList.filter(item => item.tenantId == d.tenantId);
                                return tenant[0].tenantName;
                            }

                        }
                    },
                    {field: 'rewardApplyNo', title: '申请单号', minWidth: 175 ,style: 'color:#01aaed;cursor:pointer', event: 'detail'},
                    {field: 'applyData', title: '申请时间', width: 160 },
                    {field: 'applyRealName', title: '申请人姓名', minWidth: 120, align: 'center'},
                    {field: 'rewardUser', title: '被奖励人', minWidth: 160,templet: function (d) {

                            if(d.rewardType == '1'){
                                var html= '<p>'
                                if(d.userList){
                                    d.userList.forEach((item, index)=> {
                                        html += '<em class="layui-icon febs-edit-area febs-blue">&#xe7cf;</em>'
                                        if (item.realName) {
                                            html+=item.realName
                                        }
                                        html += '&nbsp;<em class="layui-icon febs-edit-area febs-blue">&#xe835;</em>'
                                        if (item.deptName) {
                                            html+=item.deptName
                                        }
                                    })
                                }
                                html+='</p>';
                                return html;
                            }else {
                                var html = '<p style="  line-height: 17px; align-items: center;">';
                                if(d.userList){
                                    d.userList.forEach((item, index)=> {
                                        html += '<em class="layui-icon febs-blue">&#xe7cf;</em>'
                                        if (item.realName) {
                                            html+=item.realName
                                        }
                                        html += '&nbsp;<em class="layui-icon febs-edit-area febs-blue">&#xe835;</em>'
                                        if (item.deptName) {
                                            html+=item.deptName
                                        }
                                        html += '<br/>'
                                    })
                                }
                                html+='</p>';
                                return html;
                            }

                        }
                    },
                    {field: 'rewardedQuota', title: '奖励额度', width: 120,align: 'center' },
                    {
                        field: 'rewardType', title: '奖励类型', width: 120,align: 'center', templet: function (d) {
                            if (d.rewardType == '1') {
                                return '<span class="layui-badge febs-bg-gold">个人奖励</span>'
                            } else {
                                return '<span class="layui-badge febs-bg-volcano">活动奖励</span>'
                            }
                        }
                    },
                    {field: 'status', title: '状态', width: 95, templet: '#listStatusTransfer', align: 'center'},
                    {title: '操作',toolbar: '#incentiveRewardView-tool',align: 'center',width: 80,fixed: 'right'}
                ]
            ],
            toolbar: '#incentiveRewardView-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(incentiveRewardViewTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(incentiveRewardViewTable)', function (obj) {
        if (obj.event === 'export') {

        }

        if (obj.event === 'deletedView') {
            febs.modal.open('已删除奖励', 'incentiveRewardDeletedView', {
                area: $(window).width() <= 750 ? '95%' : ['1400px', '600px'],
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.closeAll();
                },
                data: {

                }
            });
        }
    });


    table.on('tool(incentiveRewardViewTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.open('删除安全奖励', 'incentiveRewardApply/delete/' + data.rewardApplyId, {
                area: $(window).width() <= 750 ? '95%' : ['1000px', '800px'],
                btn: ['删除', '取消'],
                yes: function (index, layero) {
                    $('#incentiveRewardDelete-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    source:'view'
                },
            });
        }

        if (layEvent === 'detail') {
            febs.modal.open('安全奖励信息详情', 'incentiveRewardApply/detail/' + data.rewardApplyId, {
                area: $(window).width() <= 750 ? '90%' : ['1000px', '800px'],
                shadeClose: true,
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.close(index)
                },
            });
        }
    });



    function getQueryParams() {
        return {
            tenantId: getTenantId(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            rewardUserName: $searchForm.find('input[name="rewardUserName"]').val(),
            applyUserId: $searchForm.find('select[name="applyUserId"]').find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'incentiveRewardView/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

    function getTenantId() {
        return $searchForm.find('select[name="tenantId"]').find("option:selected").val();
    }

    form.on('select(tenantId)', function(obj){
        // 获取选中项 value
        initSelect();
    });

});